package com.four.service.impl;

import com.four.service.MsmService;
import com.four.util.HttpUtils;
import com.four.util.RandomUtil;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: WangPeng
 * @createTime: 2022/10/22 10:05
 * @description:
 * @version: v1.0
 */
@Service
public class MsmServiceImpl implements MsmService {
    @Override
    public void send(String phone) {
        //调用地址：http(s)://jumsendsms.market.alicloudapi.com/sms/send-upgrade
        String host = "https://jumsendsms.market.alicloudapi.com";
        String path = "/sms/send-upgrade";
        String method = "POST";
        //自己的appcode
        String appcode = "f1946b19561542f8a5bca081e9329e3d";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        String random = RandomUtil.getFourBitRandom();
        Map<String, String> querys = new HashMap<String, String>();
        querys.put("mobile", phone);
        //使用服务商提供的测试模板id
        querys.put("templateId", "M105EABDEC");
        querys.put("value", random);
        Map<String, String> bodys = new HashMap<String, String>();

        try {
            HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
            System.out.println(response.toString());
            //获取response的body
            System.out.println(EntityUtils.toString(response.getEntity()));
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
